//
//  AFPFilterController.m
//
//  Created by Zero on 2018/8/2.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "AFPFilterController.h"
#import <WebKit/WebKit.h>
#import "Masonry.h"

#define AFPFILTERSCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
#define AFPFILTERSCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height

@interface AFPFilterController ()<WKUIDelegate,WKNavigationDelegate>{
    UIProgressView *_progress;
}
@property(nonatomic, assign)long  webLogin;
@property(nonatomic, assign)int  window_snack;


@property (strong, nonatomic) WKWebView *filterWebView;
@property (nonatomic,strong) NSMutableArray *arrayBtn;
@property (nonatomic,strong) UIView *filterbottomView;

@end

@implementation AFPFilterController

- (NSMutableArray *)arrayBtn {
    if (!_arrayBtn) {
        _arrayBtn = [NSMutableArray array];
    }
    return _arrayBtn;
}
-(float)actionPrepareLoad:(double)windowStart {
    CGFloat hexView = 522.0;
    float dysfunctionalImpressive = 0;
    hexView = hexView;
    dysfunctionalImpressive /= MAX(hexView, 1);
    
    return dysfunctionalImpressive;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    float expurgateEcosystem = [self actionPrepareLoad:6293.0];
    
    NSLog(@"%f",expurgateEcosystem);
    [self.view addSubview: self.filterWebView];
    [self colorCustomBottomButtonBar];
    
    [self.filterWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view);
        make.leading.trailing.equalTo(self.view);
        make.bottom.equalTo(self.filterbottomView.mas_top);
    }];
    
    
    _progress = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 4)];
    _progress.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_progress];
    self.navigationController.navigationBar.hidden = YES;

    NSURL * cosntantToolUrl = [[NSURL alloc] initWithString:self.filterActiveStr];
    NSURLRequest *colorRequest = [NSURLRequest requestWithURL:cosntantToolUrl];
    [self.filterWebView loadRequest:colorRequest];
    
    
}

#pragma mark ————— 创建自定义底部toolBar —————
- (void)colorCustomBottomButtonBar {
    _filterbottomView = [[UIView alloc] init];
    _filterbottomView.layer.borderWidth = 1.0;
    _filterbottomView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _filterbottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_filterbottomView];
    
    [_filterbottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view).offset(0);
        make.height.mas_equalTo(60);
    }];
    int num = 4;
    NSArray *dataArr = @[
                         @{@"title":@"",@"icon":@"filterActivehome"},
                         @{@"title":@"",@"icon":@"filterActivegoback"},
                         @{@"title":@"",@"icon":@"filterActiveforward"},
                         @{@"title":@"",@"icon":@"filterActiverefresh"}];
    for (int i = 0; i < num; i++) {
        UIButton * btn = [UIButton buttonWithType: UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:dataArr[i][@"icon"]] forState: UIControlStateNormal];
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

        btn.tag = i+1;
        [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.arrayBtn addObject:btn];
    }
    for (UIButton *btn in self.arrayBtn) {
        [_filterbottomView addSubview:btn];
    }
    
    [self.arrayBtn mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0.0f leadSpacing:0.0f tailSpacing:0.0f];
    [self.arrayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@1);
        make.bottom.equalTo(@1);
    }];
}
#pragma mark --actions
- (void)onClick:(UIButton *)sender{
    
    switch (sender.tag) {
        case 1:
        {
            if (self.filterWebView.backForwardList.backList.firstObject) {
                [self.filterWebView goToBackForwardListItem:self.filterWebView.backForwardList.backList.firstObject];
            }
        }
            break;
        case 2:
        {
            if (self.filterWebView.canGoBack) {
                [self.filterWebView goBack];
            }
        }
            break;
        case 3:
            
        {
            if (self.filterWebView.canGoForward) {
                [self.filterWebView goForward];
            }
        }
            break;
        case 4:
            
        {
            [self.filterWebView reload];
        }
            break;
        default:
            break;
    }
}
#pragma mark - WKNavigationDelegate

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    if ([navigationAction.request.URL.absoluteString containsString:@"//itunes.apple.com/"]) {
        
        [[UIApplication sharedApplication] openURL:navigationAction.request.URL];
        decisionHandler(WKNavigationActionPolicyCancel);
    }else if (navigationAction.request.URL.scheme
              && ![navigationAction.request.URL.scheme hasPrefix:@"http"]
              && ![navigationAction.request.URL.scheme hasPrefix:@"file"])
    {
        [[UIApplication sharedApplication] openURL:navigationAction.request.URL];
        decisionHandler(WKNavigationActionPolicyCancel);
    }else {
        if (navigationAction.targetFrame == nil) {
            [webView loadRequest:navigationAction.request];
        }
        decisionHandler(WKNavigationActionPolicyAllow);
    }
}

-(NSString *)applicationCustom:(CGFloat)webBtn {
    NSMutableString *feeRowIcon = [NSMutableString string];

    return feeRowIcon;

}





- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {

         NSString * iniquitous = [self applicationCustom:8988.0];

      NSLog(@"%@",iniquitous);



    [self colorDisHandleError:error];
}

-(UITableView *)onApplication{
     UIImageView * login = [[UIImageView alloc] initWithFrame:CGRectZero];
    UITableView * prudeCantataEffortless = [[UITableView alloc] initWithFrame:CGRectZero];
    login.backgroundColor = [UIColor colorWithRed:212 / 255.0 green:179 / 255.0 blue:111 / 255.0 alpha:0.8];
    login.alpha = 0.1;
    login.frame = CGRectMake(195, 26, 0, 0);
    login.image = [UIImage imageNamed:@"mainArrayMain"];
    login.contentMode = UIViewContentModeScaleAspectFit;
    login.animationRepeatCount = 5;
    prudeCantataEffortless.alpha = 0.2;
    prudeCantataEffortless.backgroundColor = [UIColor colorWithRed:95 / 255.0 green:254 / 255.0 blue:214 / 255.0 alpha:0.4];
    prudeCantataEffortless.frame = CGRectMake(226, 249, 0, 0);
    prudeCantataEffortless.dataSource = nil;
    prudeCantataEffortless.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha: 0];
    prudeCantataEffortless.delegate = nil;

    return prudeCantataEffortless;

}





- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {

         UITableView * delusive = [self onApplication];

      [self.view addSubview: delusive];



    [_progress setProgress:0.0 animated:YES];
    _progress.hidden = YES;
    [self colorDisHandleError:error];
}
-(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    _progress.hidden = NO;
    [_progress setProgress:1.0 animated:YES];
}
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [_progress setProgress:0.0 animated:YES];
    _progress.hidden = YES;
}
#pragma mark - WKUIDelegate

- (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {
    if (!navigationAction.targetFrame.isMainFrame) {
        [webView loadRequest:navigationAction.request];
    }
    return nil;
}

-(CGFloat)scanHandleTerminate:(double)strHome appRegister:(NSString *)appRegister btnNews:(float)btnNews {
     CGFloat webStr = 9334.0;
    CGFloat versus = 0;
    webStr = webStr;
    versus /= MAX(webStr, 1);

    return versus;

}





- (void)webView:(WKWebView *)webView colorJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{

         CGFloat colonizeFlu = [self scanHandleTerminate:8400.0 appRegister:@"irregular" btnNews:4016.0];

      NSLog(@"%f",colonizeFlu);



    UIAlertController *colorController = [UIAlertController alertControllerWithTitle:@"" message:message ? : @"" preferredStyle:UIAlertControllerStyleAlert];
    [colorController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        completionHandler();
    }]];
    [self presentViewController:colorController animated:YES completion:nil];
}

- (void)webView:(WKWebView *)webView colorJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler{
    UIAlertController *colorController = [UIAlertController alertControllerWithTitle:@"" message:message ? : @"" preferredStyle:UIAlertControllerStyleAlert];
    [colorController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        completionHandler(NO);
    }]];
    [colorController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        completionHandler(YES);
    }]];
    [self presentViewController:colorController animated:YES completion:nil];
}

- (void)webView:(WKWebView *)webView colorJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable))completionHandler{
    UIAlertController *colorController = [UIAlertController alertControllerWithTitle:prompt message:@"" preferredStyle:UIAlertControllerStyleAlert];
    [colorController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.text = defaultText;
    }];
    [colorController addAction:([UIAlertAction actionWithTitle:@"完成" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        completionHandler(colorController.textFields.firstObject.text ? : @"");
    }])];
    [self presentViewController:colorController animated:YES completion:nil];
}

- (void)colorDisHandleError:(NSError *)error {
    if (error.code == NSURLErrorUnsupportedURL) {
        NSString *string = error.userInfo[NSURLErrorFailingURLStringErrorKey];
        if (string) {
            NSURL *url = [NSURL URLWithString:string];
            if (url) {
                [UIApplication.sharedApplication openURL:url];
            }
        }
    }
}
#pragma mark --accessors

- (WKWebView *)filterWebView {
    if (!_filterWebView) {
        WKUserContentController *userContentController = [[WKUserContentController alloc] init];
        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
        configuration.userContentController = userContentController;
        
        _filterWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, AFPFILTERSCREEN_WIDTH,AFPFILTERSCREEN_HEIGHT) configuration:configuration];
        _filterWebView.translatesAutoresizingMaskIntoConstraints = NO;
        _filterWebView.UIDelegate = self;
        _filterWebView.navigationDelegate = self;
        if (@available(iOS 11.0, *)) {
            _filterWebView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _filterWebView.allowsBackForwardNavigationGestures = YES;
        
    }
    return _filterWebView;
}


@end
